网络工程师 Python 基础语法

您所在的位置:网站首页 函数调用 英文 网络工程师 Python 基础语法

网络工程师 Python 基础语法

2023-04-02 09:16| 来源: 网络整理| 查看: 265

大家好,我又来了!

我们重新回顾一下函数创建和调用过程,形参是在函数创建时对变量进行定义,实参则是在函数调用时将具体数值传递给变量。此前我们弄的,都是手工的一一指定,比如a=1, b=2。在Python中,表达式*args和**kwargs允许执行另一项任务——解包参数。它允许我们在调用函数,传递数据时,直接给一个Python对象,后面的事情由Python帮我们处理。

这可能有点抽象,需要我们结合实际例子来辅助理解。

〇、参考说明

本专栏简介及目录入口,如果你不知道从何读起,建议从这篇《目录》开始,链接如下:

本文部分参考书籍《Python for network engineers》,纯英文,推荐移步阅读,链接如下:

一、拆包位置参数1.1 拆包引例

我常倡导使用f字符串来做格式化,这里我们也实施format方法吧。如下面的例子,我们按照规规矩矩的写法。

>>> items = [1, 2, 3] >>> print('One: {}, Two: {}, Three: {}'.format(items[0], items[1], items[2])) One: 1, Two: 2, Three: 3 >>>

如果我们利用拆包特性呢?

>>> items = [1, 2, 3] >>> print('One: {}, Two: {}, Three: {}'.format(*items)) One: 1, Two: 2, Three: 3 >>>

一样的效果,会不会简洁很多呢?

1.2 拆包实战

另一个例子是config_interface函数(func_config_interface_unpacking.py文件),这次我们改用f字符串。

def config_interface(intf_name, ip_address, mask): interface = f'interface {intf_name}' no_shut = 'no shutdown' ip_addr = f'ip address {ip_address} {mask}' result = [interface, no_shut, ip_addr] return result

这个函数包括了三个参数:

intf_name - 端口名ip_address - IP地址mask - 子网掩码

函数会返回一个字符串列表,如果编辑模式可以这样写。

# 在代码后面增加两行。 print(config_interface('Fa0/1', '10.0.1.1', '255.255.255.0')) print(config_interface('Fa0/10', '10.255.4.1', '255.255.255.0'))

如果是对话模式可以这样写,原脚本跑起来后,直接调用函数。

如果调用一个函数,其参数是从另一个源(比如数据库)获得的,比如端口配置列表信息。

依然是func_config_interface_unpacking.py文件,我们增加一个interfaces_info。

def config_interface(intf_name, ip_address, mask): interface = f'interface {intf_name}' no_shut = 'no shutdown' ip_addr = f'ip address {ip_address} {mask}' result = [interface, no_shut, ip_addr] return result #print(config_interface('Fa0/1', '10.0.1.1', '255.255.255.0')) #print(config_interface('Fa0/10', '10.255.4.1', '255.255.255.0')) interfaces_info = [['Fa0/1', '10.0.1.1', '255.255.255.0'], ['Fa0/2', '10.0.2.1', '255.255.255.0'], ['Fa0/3', '10.0.3.1', '255.255.255.0'], ['Fa0/4', '10.0.4.1', '255.255.255.0'], ['Lo0', '10.0.0.1', '255.255.255.255']] for info in interfaces_info: print(config_interface(info))

如果使用常规的for循环,这个结果会报如下错误。

TypeError: config_interface() missing 2 required positional arguments: 'ip_address' and 'mask'

此时结合拆包概念,我们可以将for循环的内容进行修改,config_interface(info)变为config_interface(*info) ,即

for info in interfaces_info: print(config_interface(*info))

此时,运行就正常了!

Python会自动的讲列表元素进行拆包,然后进行相应传参操作。列表如此,元组也如此,同样适合拆包操作。

二、拆包关键字参数

类似的,关键字参数也可以进行拆包操作。函数check_passwd(func_check_pass_optional_param_2.py文件):

# 函数体 def check_passwd(username, password, min_length=8, check_username=True): if len(password)

参照拆包位置参数,这里读者朋友们可以尝试一下将**data改成*data或data,看看会不会报错,报什么错!

三、本文总结

你从Netmiko的一些示例文件中,常常可以看到ConnectHandler(**SW)这样的东西。如果没有这块几乎,我们其实也可以依葫芦画瓢,把一些连接资源填入SW这个字典中,然后跑起来。 如果有了拆包关键字参数的的概念以后,就能清晰的知道这个是如何对应的。

from netmiko import ConnectHandler SW = {'device_type':'huawei', 'ip':'192.168.11.12', 'username':'python', 'password':'123'} connect = ConnectHandler(**SW)

这里的ConnectHandler(**SW)实际上就等价于ConnectHandler(device_type='huawei', ip:'192.168.11.12',username:'python', password:'123')。

显然,用拆包写法,结构会清晰很多。

我读过的书、用过的物(持续更新,其中有我自己出版的书,欢迎支持)

祝贺!你终于看完了!

我全开源写作,不设任何付费阅读内容,内容有疑问随时交流,文章反复打磨!

欢迎关注点赞收藏评论交流,如果觉得特别认可,可挑一篇最喜欢的打赏 1元 支持。感谢!

发布2023年03月于广东汕头

更新2023年03月于广东汕头



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3